const { post } = require("../../utils/request")
const ApiConfig = require("../../config/api-config");

// components/drug-upload/drug-upload.js
Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    }
  },
  data: {
    formData:{
      name: '',
      expireDate: '',
      producer: '',
      pkgSpecCrit: ''
    }
  },

  methods: {
    showModal(){
      this.setData({show:true})
    },

    hideModal(){
      this.setData({show:false})
      this.setData({
        formData: {}
      })
      this.triggerEvent('close')
    },

    async onSubmit(e){
      console.log('上报的数据',this.data.formData)
      const url = ApiConfig.server.baseUrl + ApiConfig.endpoints.uploadDrugInfo;
      const res = await post(url, this.data.formData); 
      if(res.data){
        wx.showToast({ title: '上报成功', icon: 'none' });
      }else{
        wx.showToast({ title: '上报失败', icon: 'none' });
      }
      this.setData({
        formData: {}
      })
      this.hideModal()
    },

    onDateChange(e){
      this.setData({
        'formData.expireDate': e.detail.value // picker 返回的就是 'YYYY-MM-DD'
      });
    },
    handleInputName(e){
      this.setData({
        'formData.name': e.detail.value
      });
    },
    handleInputProducer(e){
      this.setData({
        'formData.producer': e.detail.value
      });
    },
    handleInputPkgSpecCrit(e){
      this.setData({
        'formData.pkgSpecCrit': e.detail.value
      });
    }
  }
})